package com.winz.cultureapp.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import androidx.core.content.FileProvider;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.http.DownloadUtils;
import com.winz.cultureapp.widge.SelVolunteerFrequencyDialog;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class VolunteerAdminActivity extends BaseAppCompatActivity {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.et_pname)
    EditText mEt_pName;
    @BindView(R.id.et_up)
    EditText mEt_up;
    @BindView(R.id.et_ptell)
    EditText mEt_pTell;
    @BindView(R.id.et_email)
    EditText mEt_email;
    @BindView(R.id.et_admin_name)
    EditText mEt_adminName;
    @BindView(R.id.et_tell)
    EditText mEt_tell;
    @BindView(R.id.et_idcard)
    EditText mEt_idCard;
    @BindView(R.id.et_all_num)
    EditText mEt_allNum;
    @BindView(R.id.et_num)
    EditText mEt_num;
    @BindView(R.id.tv_frequency)
    TextView mTv_frequency;

    private SelVolunteerFrequencyDialog mSel_frequency;
    private String frequency; // 当前选择性别记录

    private File file;
    private Uri mUriFromFile;
    private static final String FILE_PROVIDER_AUTHORITY = "com.winz.cultureapp.fileprovider";

    @Override
    protected int contentView() {
        return R.layout.activity_volunteer_admin;
    }

    @Override
    protected void initViews() {
        mTv_top_title.setText("志愿者管理账号申请");

        mSel_frequency = new SelVolunteerFrequencyDialog(this);
        mSel_frequency.setTv_title("服务频率");
    }

    @Override
    protected void initEvents() {
        mSel_frequency.setOkButtonOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSel_frequency.dismiss();
                frequency = mSel_frequency.getSelFrequency();
                mTv_frequency.setText(frequency);
            }
        });
    }

    @OnClick({R.id.fl_back, R.id.ll_frequency, R.id.btn_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.ll_frequency:
                mSel_frequency.show();
                break;
            case R.id.ll_file:
                // 下载文件
                download();
                break;
            case R.id.btn_submit:
                if (isAllRight()) {
                    showMsg("提交");
                }
                break;
        }
    }

    /**
     * 判断是否全部填写
     *
     * @return
     */
    private boolean isAllRight() {
        if (TextUtils.isEmpty(getEditTextStr(mEt_pName))) {
            showMsg("请输入单位名称");
            return false;
        }

        if (TextUtils.isEmpty(getEditTextStr(mEt_up))) {
            showMsg("请输入上级主管单位");
            return false;
        }

        if (TextUtils.isEmpty(getEditTextStr(mEt_pTell))) {
            showMsg("请输入单位联系电话");
            return false;
        }

        if (TextUtils.isEmpty(getEditTextStr(mEt_email))) {
            showMsg("请输入电子邮箱");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_adminName))) {
            showMsg("请输入账户管理员姓名");
            return false;
        }

        if (TextUtils.isEmpty(getEditTextStr(mEt_tell))) {
            showMsg("请输入手机号码");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_idCard))) {
            showMsg("请输入身份证号");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_allNum))) {
            showMsg("请输入志愿者人数");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_num))) {
            showMsg("请输入青年人数");
            return false;
        }
        if (TextUtils.isEmpty(mTv_frequency.getText())) {
            showMsg("请输入选择服务频率");
            return false;
        }
        return true;
    }

    /**
     * 下载文件
     */
    private void download() {
        file = createPdfFile();//创建用来保存照片的文件
        mUriFromFile = Uri.fromFile(file);
        showProgressDialog("文件下载中,请稍候....");
        DownloadUtils.getInstance().download("http://zheli.org/data/i20190126110347.pdf", file, new DownloadUtils.OnFinishedListener() {
            @Override
            public void onFinished(String filePath) {
                dismissProgressDialog();
                galleryAddFile(mUriFromFile);
                Intent intent = new Intent("android.intent.action.VIEW");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                Uri uri = null;
                File file = new File(filePath);
                if (file.exists()) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        //data是file类型,忘了复制过来
                        uri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, new File(filePath));

                    } else {
                        uri = Uri.fromFile(new File(filePath));
                    }
                }
                //pdf文件要被读取所以加入读取权限
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(uri, "application/pdf");
//              startActivity(intent);
                startActivity(Intent.createChooser(intent, "标题"));
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                showMsg("下载失败");
            }
        });

    }

    /**
     * 创建用来存储文件，以时间来命名就不会产生命名冲突
     *
     * @return 创建的文件
     */
    private File createPdfFile() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "volunteer_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File imageFile = null;
        try {
            imageFile = File.createTempFile(imageFileName, ".doc", storageDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageFile;
    }

    /**
     * 将下载到的文件添加系统文体列表
     *
     * @param uri
     */
    private void galleryAddFile(Uri uri) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);//ACTION_MEDIA_SCANNER_SCAN_FILE
        mediaScanIntent.setData(uri);
        sendBroadcast(mediaScanIntent);
    }
}
